﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI假死
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static Dictionary<string, Element> BuildDic()
        {
            return new Dictionary<string, Element>
            {
                {"L", new Element(){Symbol = "L", Name = "Postass", AutominNumber = 9}},
                {"Q", new Element(){ Symbol = "Q", Name = "Calcium", AutominNumber = 99}},
                {"M", new Element(){ Symbol = "JY", Name = "JYaoiang", AutominNumber=7924}}
            };
        }
        public static void IterateDictionary()
        {
            Dictionary<string, Element> element = BuildDic();
            foreach (KeyValuePair<string, Element> keyValue in element)
            {
                Element ele = keyValue.Value;
                Console.WriteLine(string.Format("Key={0}; Values={0};{1};{2}", keyValue.Key, ele.Symbol, ele.Name, ele.AutominNumber));
            }
        }

        public static Dictionary<string, Element> BuildDictionary()
        {
            var elements = new Dictionary<string, Element>();
            AddDictionary(elements, "L", "LLL", 9);
            AddDictionary(elements, "J", "LJLHHH", 19);
            AddDictionary(elements, "A", "ABABABA", 20);
            return elements;
        }

        public static void AddDictionary(Dictionary<string, Element> elements, string symbol, string name, int num)
        {
            Element ele = new Element()
            {
                Symbol = symbol,
                Name = name,
                AutominNumber = num
            };
            elements.Add(key: symbol, value: ele);


        }

        public static void FindDictionary(string symbol)
        {
            Dictionary<string, Element> elements = BuildDictionary();
            if (elements.ContainsKey(symbol))
            {
                Element ele = elements[symbol];
                Console.WriteLine("Found: " + ele.Name);
            }
            else
            {
                Console.WriteLine("Not found " + symbol);
            }
        }

        public static void FindDictionaryOfTryGetValue(string symbol)
        {
            Dictionary<string, Element> elements = BuildDictionary();
            Element ele = null;
            if (elements.TryGetValue(symbol, out ele))
            {
                Console.WriteLine("Found: " + ele.Name);
            }
            else
            {
                Console.WriteLine("Not found " + symbol);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<int, string> msgss = new Dictionary<int, string>();



            Dictionary<int, string> msgs = new Dictionary<int, string>()
{
    { 1, "Hello, "},
    { 2 , "World"},
    { 2, "!"}
};


            Dictionary<int, string> MsgErrs = new Dictionary<int, string>()
            {
                [1] = "Hello, ",
                [2] = "World2",
                [2] = "World3",
                [4] = "World4",
            };


        }

        private void Start_OnExeception(object obj)
        {
            ConcurrentDictionary<string, string> errMap = obj as ConcurrentDictionary<string, string>;
        }

        private async Task<string> GetMessage()
        {
            return await Task<string>.Run(() =>
            {
                Thread.Sleep(10 * 1000);
                return "Hello World";

            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread objThread = new Thread(new ThreadStart(delegate
            {
                ThreadMethodTxt(Convert.ToInt32(textBox1.Text.Trim()));
            }));
            objThread.Start();
        }

        //创建一个委托，是为访问TextBox控件服务的。
        public delegate void UpdateTxt(string msg);
        //定义一个委托变量
        public UpdateTxt updateTxt;

        //修改TextBox值的方法。
        public void UpdateTxtMethod(string msg)
        {
            MethodInvoker me = delegate ()
            {
                richTextBox1.AppendText(msg + "\r\n");
                richTextBox1.ScrollToCaret();
            };

            this.Invoke(me, null);
        }

        //此为在非创建线程中的调用方法，其实是使用TextBox的Invoke方法。
        public void ThreadMethodTxt(int n)
        {
            //this.BeginInvoke(updateTxt, "线程开始执行，执行" + n + "次，每一秒执行一次");
            //for (int i = 0; i < n; i++)
            //{
            //    this.BeginInvoke(updateTxt, i.ToString());
            //    //一秒 执行一次
            //    Thread.Sleep(1000);
            //}
            //this.BeginInvoke(updateTxt, "线程结束");


            updateTxt("线程开始执行，执行" + n + "次，每一秒执行一次");
            for (int i = 0; i < n; i++)
            {
                updateTxt(i.ToString());
                //一秒 执行一次
                Thread.Sleep(1000);
            }
            updateTxt("线程结束");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //实例化委托
            updateTxt = new UpdateTxt(UpdateTxtMethod);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int[] xxx = new int[1024 * 1024 * 3];

            for (int a = 0; a < 100; a++)
            {
                Console.WriteLine("a=" + a);

                xxx = new int[1024 * 1024 * 3];
            }
        }
    }
}
